package com.gis.school.gisserver.service.impl;


import com.gis.school.dto.UserDtoLogin;
import com.gis.school.result.Result;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gis.school.gisserver.service.IUserService;
import com.gis.school.entity.UserEntity;
import com.gis.school.gisserver.mapper.UserMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gis.school.constant.MessageConstant.*;

/**
 * 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<String> loginService(String uname, String password) {
        Map<String, Object> params = new HashMap<>();
        params.put("username", uname);
        params.put("password", password);
        List<UserEntity> users =userMapper.selectListByMap(params);
        if(!users.isEmpty()){
            return Result.success(LOGIN_SUCCESS);
        }
        return Result.error(LOGIN_ERROR);
    }

    @Override
    public Result<String> registService(UserDtoLogin newUser) {
        Map<String, Object> params = new HashMap<>();
        params.put("username", newUser.getUsername());
        System.out.println(newUser.getUsername());
        if (!userMapper.selectListByMap(params).isEmpty()){
            return Result.error(REGIST_ERROR);
        }
        UserEntity user = new UserEntity();
        user.setUsername(newUser.getUsername());
        user.setPassword(newUser.getPassword());
        userMapper.insert(user);
        return Result.success(REGIST_SUCCESS);

    }
}